package cn.exrick.xboot.modules.news.controller;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.jiuqi.utils.ServeUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.news.entity.NewsImages;
import cn.exrick.xboot.modules.news.service.INewsImagesService;
import cn.exrick.xboot.modules.portalConfig.dao.mapper.PortalConfigMapper;
import cn.exrick.xboot.modules.portalConfig.entity.PortalConfig;
import cn.exrick.xboot.modules.portalConfig.service.IPortalConfigService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "测试管理接口")
@RequestMapping("/xboot/newsImages")
@Transactional
public class NewsImagesController {

    @Autowired
    private INewsImagesService iNewsImagesService;

    @Autowired
    private IPortalConfigService iPortalConfigService;

    @Value("${server.port}")
    private String port;
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<NewsImages> get(@PathVariable String id){

        NewsImages newsImages = iNewsImagesService.getById(id);
        return new ResultUtil<NewsImages>().setData(newsImages);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<NewsImages>> getAll(){

        List<NewsImages> list = iNewsImagesService.list();
        return new ResultUtil<List<NewsImages>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<NewsImages>> getByPage(@ModelAttribute PageVo page){

        IPage<NewsImages> data = iNewsImagesService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<NewsImages>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<NewsImages> saveOrUpdate(@ModelAttribute NewsImages newsImages){
        newsImages.setUpdateTime(new Date());
        if(iNewsImagesService.saveOrUpdate(newsImages)){
            return new ResultUtil<NewsImages>().setData(newsImages);
        }
        return new ResultUtil<NewsImages>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iNewsImagesService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/getImgsByNewsIds/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过新闻id获取图片信息")
    public Result<String> getImgsByNewsIds(@PathVariable String id){
        //获取访问前缀
        /*PortalConfig config  = iPortalConfigService.selectPortalConfigByKey("url");
        String url=config.getValue();*/

        /*InetAddress address = null;//获取的是本地的IP地址
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }*/
        //String IP = address.getHostAddress();//192.168.0.121
        String IP =ServeUtil.getInet4Address();
        NewsImages newsImages1 = iNewsImagesService.selectNewsImagesByNewsId(id);
        String name=null;
        if(newsImages1!=null){
        /*String imagePath = newsImages1.getImagePath();//"/update/"*/
         name = newsImages1.getName();
        }else {
            log.info("返回图片默认访问地址：../../assets/left-l.png");
            /**
             *  njp 20200224获取资源服务器的port的改造 begin
             */
            String  fileProjectPort =ServeUtil.getFilrProject();
            /**
             *  njp 20200224 获取资源服务器的port的改造 end
             */
            return new ResultUtil<String>().setData("http://"+IP+":"+fileProjectPort+CommonConstant.IMAGE_PATH+"default.jpg");
        }
        //String imgUrl="http://"+IP+":"+port+CommonConstant.IMAGE_PATH+newFileName;
        //拼接访问地址：  前缀++图片名字
        //String getImage=url+ CommonConstant.IMAGE_PATH+name;
        String getImage=  "http://"+IP+":"+port+CommonConstant.IMAGE_PATH+name;
        log.info("图片访问地址："+getImage);
        return new ResultUtil<String>().setData(getImage);
    }


}
